initflag()
{
	setFlags();
}

getCoor(var)
{
	// if var=="base" then it returns the coordinates for the objective base
	// if var=="obj" then it returns a random location for the objective



	if(getcvar("mapname") == "mp_sabes_du_mot_beta")
	{
		//first set whether we should switch spawns around on this map:
		level.re_switch_spawns = true;

		if(var=="base") {
			return (134,-2481,140);
		}
		if(var=="obj") {
			objposArray[0] = (3172,1971,175);	//toilet
			objposArray[1] = (4767,414,199);	//ruins
			objposArray[2] = (3194,128,256);	//white house
			objposArray[3] = (4042,-2285,203);	//boxes
			rand=randomint(100);
			return objposArray[rand%4];
		}
	}
	else if(getcvar("mapname") == "mp_romed")
	{
		if(var == "base"){
			return (2,-2220,82);
		}
		if(var == "obj"){
			objposArray[0] = (1,867,82);		//building
			objposArray[1] = (1182,1095,176);	//roofs
			objposArray[2] = (-939,1206,41);	//toilet
			objposArray[3] = (1,-621,-391);		//tunnels
			rand=randomint(100);
			return objposArray[rand%4];
		}
	}
	else if(getcvar("mapname") == "mp_omaha_v2a")
	{
		//first set whether we should switch spawns around on this map:
		level.re_switch_spawns = true;

		if(var == "base"){
			return (506,-2347,-394);
		}
		if(var == "obj"){
			objposArray[0] = (-914,-528,258);	//West bunker
			objposArray[1] = (-1328,428,440);	//West crates
			objposArray[2] = (348,1257,291);	//Back bunker
			objposArray[3] = (424,991,292);		//Barrel
			objposArray[4] = (980,687,-319);	//Ladder room
			objposArray[5] = (1836,142,100);	//Desk
			objposArray[6] = (-1559,2290,428);	//Back crates

			rand=randomint(103);
			//iprintlnbold("Random int: " + rand);
			//iprintlnbold("Choosing obj: " + rand%7);
			return objposArray[rand%7];
		}
	}



/*  //This is a clean version of the necessary things to add a map.  Just copy and fill in the blanks.
	elseif(getcvar("mapname") == "")
	{
		if(var == "base"){
			return ();
		}
		if(var == "obj"){
			objposArray[0] = ();	//Location details
			objposArray[1] = ();	//Location details
			objposArray[2] = ();	//Location details
			objposArray[3] = ();	//Location details
			rand=randomint(100);
			return objposArray[rand%4];
		}
	}

*/
	//this should never happen
	return (0,0,0);
}


setFlags() 
{
	objLocation = getCoor("obj");
	objLocation2 = getCoor("obj");
	while(objLocation2 == objLocation) {
		objLocation2 = getCoor("obj");
	}

	allied_flag = getent("allied_flag", "targetname");
	allied_flag.home_origin = objLocation;
	allied_flag.home_angles = allied_flag.angles;
	allied_flag.flagmodel = spawn("script_model", allied_flag.home_origin);
	allied_flag.flagmodel.angles = allied_flag.home_angles;
	allied_flag.flagmodel setmodel("xmodel/prop_map_3");
	allied_flag.team = game["attackers"];
	allied_flag.atbase = true;
	allied_flag.objective = 0;
	allied_flag.compassflag = level.compassflag_allies;
	allied_flag.objpointflag = level.hudflag_allies;
	allied_flag thread objective(objLocation);

	axis_flag = getent("axis_flag", "targetname");
	axis_flag.home_origin = getCoor("base");
	axis_flag.home_angles = axis_flag.angles;
	axis_flag.flagmodel = spawn("script_model", axis_flag.home_origin);
	axis_flag.flagmodel.angles = axis_flag.home_angles;
	axis_flag.flagmodel setmodel("xmodel/prop_map_case_1");
	axis_flag.team = game["defenders"];
	axis_flag.atbase = true;
	axis_flag.objective = 1;
	axis_flag.compassflag = level.compassflag_axis;
	axis_flag.objpointflag = level.hudflag_axis;
	axis_flag thread base(getCoor("base"));


	spawnpoints = getentarray("mp_dm_spawn", "classname");
	j = randomInt(spawnpoints.size);
	flag2 = spawnpoints[j];
	flag2.home_origin = objLocation2;
	flag2.home_angles = allied_flag.home_angles;
	flag2.flagmodel = spawn("script_model", objLocation2);
	flag2.flagmodel.angles = flag2.home_angles;
	flag2.flagmodel setmodel("xmodel/prop_map_3");
	flag2.team = game["attackers"];
	flag2.atbase = true;
	flag2.objective = 2;
	flag2.compassflag = level.compassflag_allies;
	flag2.objpointflag = level.hudflag_allies;
	flag2 thread objective(objLocation2);
}

objective(coor)
{
	//set coordinates for waypoint
	self.origin = coor;
	objective_add(self.objective, "current", self.origin, self.compassflag);
	self maps\mp\gametypes\re::createFlagWaypoint();


	for(;;)
	{
		//self waittill("trigger", other);
		other = WaitForRadius(self.origin, 60, 50);

		// [BP] Prevent pickup at warmup
		if (game["mode"] != "battle" || (game["mode"] == "battle" && game["live"]))
		{

			if(isPlayer(other) && isAlive(other) && (other.pers["team"] != "spectator"))
			{
				if(other.pers["team"] == self.team) // Touched by own team
				{
					if(!self.atbase) { //return obj
					
					} 
				}
				else if(other.pers["team"] != self.team && !isdefined(other.flag)) // Touched by enemy
				{
					if(self.team == "axis")
						enemy = "allies";
					else
						enemy = "axis";

					thread maps\mp\gametypes\re::playSoundOnPlayers("objtaken", "allies");
					if(!level.splitscreen)
						thread maps\mp\gametypes\re::playSoundOnPlayers("objtaken", "axis");

					thread maps\mp\gametypes\re::printOnTeam(&"RE_YOUR_FLAG_WAS_TAKEN", self.team);
					thread maps\mp\gametypes\re::printOnTeam(&"RE_ENEMY_FLAG_TAKEN", enemy);

					other maps\mp\gametypes\re::pickupFlag(self); // Stolen flag
				}
			}
			wait 0.05;
		}
	}
}

base(coor)
{	
	//set coordinates for waypoint
	self.origin = coor;
	objective_add(self.objective, "current", self.origin, self.compassflag);
	self maps\mp\gametypes\re::createFlagWaypoint();


	for(;;)
	{
		self waittill("trigger", other);

		if(isPlayer(other) && isAlive(other) && (other.pers["team"] != "spectator"))
		{
			if(other.pers["team"] == self.team) // Touched by team
			{
				if(self.atbase)
				{
					if(isdefined(other.flag)) // Captured flag
					{
						friendlyAlias = "ctf_touchcapture";
						enemyAlias = "ctf_enemy_touchcapture";

						if(self.team == "axis")
							enemy = "allies";
						else
							enemy = "axis";

						thread maps\mp\gametypes\re::playSoundOnPlayers(friendlyAlias, self.team);
						if(!level.splitscreen)
							thread maps\mp\gametypes\re::playSoundOnPlayers(enemyAlias, enemy);

						thread maps\mp\gametypes\re::printOnTeam(&"RE_ENEMY_FLAG_CAPTURED", self.team);
						thread maps\mp\gametypes\re::printOnTeam(&"RE_YOUR_FLAG_WAS_CAPTURED", enemy);

						other.flag maps\mp\gametypes\re::returnFlag();
						other maps\mp\gametypes\re::detachFlag(other.flag);
						other.flag = undefined;

						other.pers["score"]+=5;
						other.score = other.pers["score"];

						if(game["attackers"]=="allies") 
						{
							teamscore = getTeamScore("axis");
							teamscore += 1;
							setTeamScore("axis", teamscore);
							level notify("update_teamscore_hud");

							iprintln(&"MP_AXISMISSIONACCOMPLISHED");
							level thread maps\mp\gametypes\re::endRound("axis");
						}
						else
						{
							teamscore = getTeamScore("allies");
							teamscore += 1;
							setTeamScore("allies", teamscore);
							level notify("update_teamscore_hud");

							iprintln(&"MP_ALLIEDMISSIONACCOMPLISHED");
							level thread maps\mp\gametypes\re::endRound("allies");
						}
						

						maps\mp\gametypes\re::checkScoreLimit();
					}
				}
				
			}
			
		}
		wait 0.05;
	}
}


WaitForRadius(origin, radius, height) 
{
	if(!isDefined(origin) || !isDefined(radius) || !isDefined(height)) return;

	trigger = spawn("trigger_radius", origin, 0, radius, height);	

	while(1) 
	{
		trigger waittill("trigger", other);	
	
		if(isPlayer(other) && other.sessionstate == "playing") 
		{
			if(isDefined(trigger)) trigger delete();
			return other;
		}
		
		wait 0.1;
	}
	
	if(isDefined(trigger)) trigger delete();
}
